/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sh.ndt.login.endpoint;

import com.sh.ndt.common.entity.LoginParam;
import com.sh.ndt.exception.ServiceException;
import com.sh.ndt.user.entity.User;
import com.sh.ndt.user.entity.UserDashboardDetail;
import com.sh.ndt.user.entity.UserEntitlement;
import com.sh.ndt.user.entity.UserParam;
import com.sh.ndt.user.service.UserEntitlementService;
import com.sh.ndt.user.service.UserService;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 *
 * @author Prashanth
 */
@Controller
@RequestMapping("/loginService")
public class LoginEndpoint {
    private static final Logger LOGGER = Logger.getLogger(LoginEndpoint.class);
    
    public LoginEndpoint(){
        LOGGER.debug("LoginEndpoint is initialized ********************** ");
    }
    
    @Autowired
    UserService userService;
    
    @Autowired
    UserEntitlementService userEntitlementService;
    
    
    @RequestMapping(value = "/authenticateUser",
    method = RequestMethod.POST,
    consumes = "application/xml",
    produces = "application/json"
    )
    @ResponseBody
    public UserEntitlement authenticateUser(@RequestBody LoginParam loginParam) {
        
        List<User> userList = new ArrayList<User>();
                
        try {
            LOGGER.debug("Calling LoginEndpoint's authenticateUser method...");
            
            LOGGER.debug("Users Email : " + loginParam.getEmailId());
            LOGGER.debug("Password: " + loginParam.getPassword());
           
            userList =  userService.validateUser(loginParam);
           
            LOGGER.debug("Size: " + userList.size());
           
            LOGGER.debug("Users First Name: " + userList.get(0).getFirstName());
            LOGGER.debug("Users Last Name: " + userList.get(0).getLastName());
            
            UserEntitlement userEntitilement = new UserEntitlement();
            
            // Set User
            userEntitilement.setUser(userList.get(0)); 
            // Set User's Entitlement (Dashboard Details)
            userEntitilement.setUserDashboardDetail(userEntitlementService.getUserDashboardDetail(userList.get(0).getUserId()));
            
            return userEntitilement;
            
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    
    @RequestMapping(value = "/getUserEntitlement",
    method = RequestMethod.POST,
    consumes = "application/xml",
    produces = "application/json"
    )
    @ResponseBody
    public UserEntitlement getUserEntitlement(@RequestBody UserParam userParam){
        try {
            return userEntitlementService.loadUserEntitlement(userParam.getUserId());
        } catch (ServiceException ex) {
            LOGGER.debug(ex);
        }
        return null;
    }
 
    @RequestMapping(value = "/getDashboardDetail",
    method = RequestMethod.POST,
    consumes = "application/xml",
    produces = "application/json"
    )
    @ResponseBody
    public UserDashboardDetail getDashboardDetail(@RequestBody UserParam userParam){
        try {
            return userEntitlementService.getUserDashboardDetail(userParam.getUserId());
        } catch (ServiceException ex) {
            LOGGER.debug(ex);
        }
        return null;
    }    
}
